import request, { filterUsefulRequestParams } from '@/utils/request';

export const getNewFunctionList = params =>
    request('/api/oa/uac/newFunction/list', {
        method: 'POST',
        data: params
    });

export const getNewFunctionDetail = params =>
    request('/api/oa/uac/newFunction/resource/detail', {
        method: 'POST',
        data: params
    });

export const getNewFunctionDetailTree = params =>
    request('/api/oa/uac/newFunction/resource/detail/tree', {
        method: 'POST',
        data: params
    });

export const getAllRoleData = tenantCode =>
    request('/api/oa/uac/role/v1/list4quote', {
        method: 'POST',
        data: tenantCode ? { tenantCode } : null
    });

/**
 * @description 权限模板--管理列表查询
 * @param {Object} params {keyword:''}
 */
export const getPrivilegeTemplate = params =>
    request('/api/oa/uac/role/v1/list4manage', {
        method: 'POST',
        data: params
    });

/**
 * @description 权限模板—查看详情
 * @param {String} roleCode 模块code
 */
export const getPrivilegeTemplateDetail = roleCode =>
    request(`/api/oa/uac/role/v1/detail4view/${roleCode}`, {
        method: 'POST'
    });

/**
 * @description 权限模板--编辑详情
 * @param {String} roleCode 模块code
 */
export const getPrivilegeTemplateEditInfo = roleCode =>
    request(`/api/oa/uac/role/v1/detail4edit/${roleCode}`, {
        method: 'POST'
    });

/**
 * @description 权限模板--仅保存
 */
export const saveRoleInDraftMode = params =>
    request('/api/oa/uac/role/v1/saveRoleInDraftMode', {
        method: 'POST',
        data: filterUsefulRequestParams(params)
    });

/**
 * @description 权限模板—保存并提交(返回hiDataId)
 */
export const saveRoleInSubmitMode = params =>
    request('/api/oa/uac/role/v1/saveRoleInSubmitMode', {
        method: 'POST',
        data: params
    });

/**
 * @description 获取租户的功能权限列表
 */
export const getPrivilegeResources = params =>
    request('/api/oa/uac/role/v1/tenant/resources', {
        method: 'POST',
        data: params
    });

/**
 * @description 权限模板—删除并提交(返回hiDataId)
 */
export const deleteRoleInSubmitMode = roleCode =>
    request(`/api/oa/uac/role/v1/deleteRoleInSubmitMode/${roleCode}`, {
        method: 'POST'
    });

export const newFunctionSaveHiData = params =>
    request('/api/oa/workflow/newFunction/v1/savehidata', {
        method: 'POST',
        data: params
    });

/**
 * 发布暂存
 * @param {Object} params
 */
export const newFunctionDeployDraft = params =>
    request('/api/oa/uac/newFunction/save/draft', {
        method: 'POST',
        data: params
    });

/**
 * 获取暂存信息
 * @param {Object} params
 */
export const getNewFunctionDeployDraft = params =>
    request('/api/oa/uac/newFunction/get/draft', {
        method: 'GET',
        params
    });

export const setNewFunctionRead = params =>
    request('/api/oa/uac/newFunction/read', {
        method: 'POST',
        data: params
    });

export const setUsedIcons = () =>
    request('/api/oa/uac/role/v1/tenant/usedIcons', {
        method: 'GET'
    });

/**
 *获取功能权限列表
 *
 * @export POST
 * @params {string} keyword 搜索内容
 * @returns
 */
export async function fetchFunctionList(params) {
    return request('/api/uc/uac/resource/v1/tenant/resources', {
        method: 'POST',
        data: params
    });
}

/**
 *加入历史数据
 *
 * @export POST
 */
export async function addHidata(params) {
    return request('/api/oa/workflow/authority/v1/savehidata', {
        method: 'POST',
        data: params
    });
}

/**
 *保存草稿
 *
 * @export POST
 */
export async function saveDraft(params) {
    return request('/api/uc/uac/resource/v1/tenant/resource/draft', {
        method: 'POST',
        data: params
    });
}

/**
 *获取模板列表
 *
 * @export POST
 */
export async function fetchManageList() {
    return request('/api/oa/uac/role/v1/list4quote', {
        method: 'POST'
    });
}

/**
 *带权限的获取模板列表
 *
 * @export POST
 */
export async function getManageListAuth() {
    return request('/api/oa/uac/role/v1/currentUserMainDutyRoles', {
        method: 'POST'
    });
}

/**
 * 获取模板列表, 但对没有数据数据权限的数据做标识
 *
 * @export POST
 */
export async function getManageListAuth_AllData() {
    const [{ data: allData }, { data: currentUserData }] = await Promise.all([fetchManageList(), getManageListAuth()]);
    return allData?.map(item => {
        const cloneItem = { ...item };
        const hasAuthData = currentUserData.find(data => data.roleTypeName === item.roleTypeName);
        cloneItem.roles = item.roles.map(role => {
            const cloneRole = { ...role };
            cloneRole.disabled = !hasAuthData?.roles.find(m => m.code === role.code);
            return cloneRole;
        });
        if (!cloneItem.roles.some(role => !role.disabled)) {
            cloneItem.disabled = true
        }
        return cloneItem;
    });
}
